'use strict';

var directives = angular.module('simpleApp.directives', []);

directives.directive('butterbar', ['$rootScope',
    function($rootScope) {
          return {
            link: function(scope, element, attrs) {
              element.addClass('hide');

              $rootScope.$on('$routeChangeStart', function() {
                element.removeClass('hide');
              });

              $rootScope.$on('$routeChangeSuccess', function() {
                element.addClass('hide');
              });
            }
    };
}]);

/*directives.directive('butter', ['$rootScope',
    function($rootScope) {
        return {
            link: function(scope, element, attrs) {
                element.addClass('hide');
            }
        };
    }]);*/




directives.directive('notloginhide', ['$rootScope','$window',
    function($rootScope,$window) {
        return {
            link: function(scope, element, attrs) {
                element.addClass('hide');
                $rootScope.$on('$routeChangeSuccess', function() {
                    if($window.sessionStorage["systemStatus"] != null)
                    {
                        var temp = JSON.parse($window.sessionStorage["systemStatus"]);
                        if(temp.login)
                        {
                            element.removeClass('hide');
                        }
                    }
                });
           }
        };
    }]);

directives.directive('notmanagerhide', ['$rootScope','$window',
    function($rootScope,$window) {
        return {
            link: function(scope, element, attrs) {
                element.addClass('hide');
                $rootScope.$on('$routeChangeSuccess', function() {
                    if($window.sessionStorage["systemStatus"] != null)
                    {
                        var temp = JSON.parse($window.sessionStorage["systemStatus"]);
                        //$rootScope.username = temp.username;
                        if(temp.login&&temp.isManager)
                        {
                            element.removeClass('hide');
                        }
                    }
                });
            }
        };
    }]);

directives.directive('focus',
    function() {
  return {
    link: function(scope, element, attrs) {
      element[0].focus();
    }
  };
});

/*directives.directive('pwCheck', [function () {
    return {
        require: 'ngModel',
        link: function (scope, elem, attrs, ctrl) {
            var firstPassword = '#' + attrs.pwCheck;
            elem.add(firstPassword).on('keyup', function () {
                scope.$apply(function () {
                    var v = elem.val()===$(firstPassword).val();
                    ctrl.$setValidity('pwmatch', v);
                });
            });
        }
    }
}]);*/

/*
directives.directive('pwCheck', [function () {
    return {
        require: "ngModel",
        link: function(scope, elem, attrs, ctrl) {
            console.log(attrs);
            var otherInput = elem.inheritedData("$formController")[attrs.pwCheck];

            ctrl.$parsers.push(function(value) {
                if(value === otherInput.$viewValue) {
                    ctrl.$setValidity("repeat", true);
                    return value;
                }
                ctrl.$setValidity("repeat", false);
            });

            otherInput.$parsers.push(function(value) {
                ctrl.$setValidity("repeat", value === ctrl.$viewValue);
                return value;
            });
        }
    };
}]);

*/
